<%--
  Created by IntelliJ IDEA.
  User: Rui
  Date: 2018/9/8
  Time: 9:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html style="background-color: #fff;">
<head>
    <%@include file="../../../../resources/inc/head.jsp" %>
</head>
<body>
<expect-x:tip tipMessage="标识“*”的选项为必填项，其余为选填项。;请按提示信息填写每一个字段" widthClass="full-width-85"/>

<div class="layui-form common-form" lay-filter="layuiadmin-form-paymentChannel" id="layuiadmin-form-paymentChannel"
     style="padding: 20px;">

    <div class="layui-form-item">
        <input type="hidden" value="${merchantApplication.id}" name="id">
        <label class="layui-form-label"><span class="require-field">*</span>应用名称</label>
        <div class="layui-input-inline">
            <input type="text" value="${merchantApplication.name}" maxlength="45" name="name" lay-verify="required"
                   placeholder="请输入名称" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="require-field">*</span>场景</label>
        <div class="layui-input-inline">
            <expect-x:dictSelect field="scene" typeGroupCode="use_scene" defaultVal="1"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="require-field">*</span>类型</label>
        <div class="layui-input-inline">
            <expect-x:dictSelect field="type" typeGroupCode="use_type" defaultVal="1"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="require-field">*</span>描述</label>
        <div class="layui-input-inline">
            <textarea  type="text/plain" class="layui-textarea"  name="introduction" lay-verify="required" placeholder="请输入" autocomplete="off">${merchantApplication.introduction}</textarea>
        </div>
    </div>
    <div class="layui-form-item" pane="">
        <label class="layui-form-label"><span class="require-field">*</span>应用渠道</label>
        <div class="layui-input-block">
            <c:forEach items="${paymentChannels}" var="payment">
                <c:choose>
                    <c:when test="${fn:contains(merchantApplication.paymentChannel, payment.id)}">
                        <input type="checkbox" name="paymentChannel" lay-skin="primary" title="${payment.name}"
                               value="${payment.id}" checked>
                    </c:when>
                    <c:otherwise>
                        <input type="checkbox" name="paymentChannel" lay-skin="primary" title="${payment.name}"
                               value="${payment.id}">
                    </c:otherwise>
                </c:choose>
            </c:forEach>
        </div>

    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="LAY-submit" id="LAY-submit" value="确认">
    </div>
</div>

<%@include file="../../../../resources/inc/footer.jsp" %>
<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , upload = layui.upload;
    })

    function createSubmit() {

        var layer = layui.layer;
        var submitID = 'LAY-submit';
        layui.form.on('submit(' + submitID + ')', function (data) {
            var formData = data.field;
            /*获取checkbox[name='paymentChannel']的值*/
            var arr = new Array();
            $("input:checkbox[name='paymentChannel']:checked").each(function (i) {
                arr[i] = $(this).attr("value");
            });
            if (arr.length==0){
                layer.msg("请选择应用渠道")
                return
            }
            formData.paymentChannel = arr.join(",");
            layer.load(2);
            console.log(formData);
            if (formData.id == "" || formData.id == null) {
                formData.id = null;
                var merchantNo = "${merchantNo}"
                $.ajax({
                    url: "<%=basePath%>manager/merchant/applicationConfig/create/"+merchantNo+".html",
                    type: "POST",
                    data: formData,
                    dataType: "JSON",
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.success) {
                            layer.msg("创建成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }
                    }
                });
            } else {
                $.ajax({
                    url: "<%=basePath%>manager/merchant/applicationConfig/update/" + formData.id + ".html",
                    type: "POST",
                    data: formData,
                    dataType: "JSON",
                    success: function (data) {
                        layer.closeAll('loading');
                        if (data.success) {
                            layer.msg("修改成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            }, 1000);
                        } else {
                            layer.msg(data.message, {icon: 5});
                        }
                    }
                });
            }
        })
        $("#" + submitID).trigger('click');
    }
</script>
</body>
</html>
